<?php
namespace app\controllers;


use app\common\corelib\LocalUpload;
use app\logic\CategoryLogic;
use app\logic\OrderLogic;
use PHPExcel_Cell_DataType;
use yii;

class OrderController extends BackendController
{

    public function actionIndex()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $orderLogic = new OrderLogic();
        $this->data['breadcrumbs'] = '订单列表';
        $categoryLogic = new CategoryLogic();
        $category = $categoryLogic->getCategorys();
        if(Yii::$app->request->get('submit')!="export"){
            return $this->render('index', [
                'theme' => '/assets/',
                'category'=>$category,
                'order' => $orderLogic->getOrder($query, $page),
                'query' => $query
            ]);
        }else{
            $order = $orderLogic->getOrder($query,1,null);
            $objPHPExcel = new \PHPExcel();
            $objPHPExcel->getProperties()->setCreator("中经汇通")
                ->setTitle("订单列表");
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '订单号')
                ->setCellValue('B1', '支付渠道')
                ->setCellValue('C1', '支付渠道订单号')

                ->setCellValue('D1', '商品名称')
                ->setCellValue('E1', '商品编号')

                ->setCellValue('F1', '商品数量')

                ->setCellValue('G1', '商品单价')
                ->setCellValue('H1', '订单金额')
                ->setCellValue('I1', '商品分类')
                ->setCellValue('J1', '用户手机号')
                ->setCellValue('K1', '身份证')

                ->setCellValue('L1', '收货人姓名')
                ->setCellValue('M1', '收货地址')
                ->setCellValue('N1', '下单时间')
                ->setCellValue('O1', '支付时间')

                ->setCellValue('P1', '订单状态')
                ->setCellValue('Q1', '寄送状态')
                ->setCellValue('R1', '退款状态')
                ->setCellValue('S1', '用户留言');
            $status = "";
            foreach($order['lists'] AS $ke=>$va){
                switch($va['order_status']){
                    case 0:
                        $order_status = "待支付";
                        break;
                    case 1:
                        $order_status = "已付款";
                        break;
                }
                switch($va['shipping_status']){
                    case 0:
                        $shipping_status = "待寄送";
                        break;
                    case 1:
                        $shipping_status = "已寄送";
                        break;
                }
                switch($va['refund_status']){
                    case 0:
                        $refund_status = "不需退款";
                        break;
                    case 1:
                        $refund_status = "申请退款";
                        break;
                    case 2:
                        $refund_status = "退款中";
                        break;
                    case 3:
                        $refund_status = "拒绝退款";
                        break;
                    case 4:
                        $refund_status = "完成退款";
                        break;
                }
                $keys = $ke+2;
                $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValueExplicit('A'.$keys, $va['order_no'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue('B'.$keys, $va['pay_type'])
                    ->setCellValueExplicit('C'.$keys, $va['pay_no'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue('D'.$keys, $va['goods_name'])
                    ->setCellValue('E'.$keys, $va['goods_id'])
                    ->setCellValue('F'.$keys, $va['goods_num'])
                    ->setCellValue('G'.$keys, $va['real_price'])
                    ->setCellValue('H'.$keys, $va['order_amount'])
                    ->setCellValue('I'.$keys, $va['category_name'])
                    ->setCellValueExplicit('J'.$keys, $va['mobile'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValueExplicit('K'.$keys, $va['id_card'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue('L'.$keys, $va['consignee'])
                    ->setCellValue('M'.$keys, $va['shipping_address'])
                    ->setCellValue('N'.$keys, $va['order_time'])
                    ->setCellValue('O'.$keys, $va['pay_time'])
                    ->setCellValue('P'.$keys, $order_status)
                    ->setCellValue('Q'.$keys, $shipping_status)
                    ->setCellValue('R'.$keys, $refund_status)
                    ->setCellValue('S'.$keys, $va['user_remark']);
            }
            $objPHPExcel->setActiveSheetIndex(0);

            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            header('pragma:public');
            header("Content-Disposition:attachment;filename=订单列表".date('_YmdHis').".xlsx");
            $objWriter->save('php://output');exit;

        }

    }

    public function actionShipping(){
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $orderLogic = new OrderLogic();
        $this->data['breadcrumbs'] = '订单列表';
        $categoryLogic = new CategoryLogic();
        $category = $categoryLogic->getCategorys();
        if(Yii::$app->request->get('submit')!="export" && Yii::$app->request->post('submit')!="import"){
            return $this->render('shipping', [
                'theme' => '/assets/',
                'category'=>$category,
                'order' => $orderLogic->getShippingOrder($query, $page),
                'query' => $query
            ]);
        }elseif(Yii::$app->request->post('submit')=="import"){
            if($_FILES['shipping_file']['name']){
                LocalUpload::go_upload_file($_FILES['shipping_file']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $objPHPExcel = new \PHPExcel();
                $objPHPExcel = \PHPExcel_IOFactory::load(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$uploadresult);
                $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
                foreach($sheetData AS $key=>$value){
                    if(isset($value['D']))
                        $orderLogic->shippingOrder1($value['A'],$value['B'],$value['C'],$value['D']);
                    else
                        $orderLogic->shippingOrder1($value['A'],$value['B'],$value['C']);
                }
            }else{
                $uploadresult = '';
            }

        }else{
            $order = $orderLogic->getShippingOrder($query,1,null);
            $objPHPExcel = new \PHPExcel();
            $objPHPExcel->getProperties()->setCreator("中经汇通")
                ->setTitle("寄件列表");
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '订单号')
                ->setCellValue('B1', '商品名称')
                ->setCellValue('C1', '商品编号')
                ->setCellValue('D1', '商品数量')
                ->setCellValue('E1', '商品单价')
                ->setCellValue('F1', '订单金额')
                ->setCellValue('G1', '商品分类')
                ->setCellValue('H1', '订单时间')
                ->setCellValue('I1', '收件人手机号')
                ->setCellValue('J1', '收件人姓名')
                ->setCellValue('K1', '收件人地址')
                ->setCellValue('L1', '身份证号')
                ->setCellValue('M1', '订单来源')
                ->setCellValue('N1', '用户留言')
                ->setCellValue('O1', '寄送状态')
                ->setCellValue('P1', '快递公司')
                ->setCellValue('Q1', '快递单号')
                ->setCellValue('R1', '寄件时间')
                ->setCellValue('S1', '备注');
            $status = "";
            foreach($order['lists'] AS $ke=>$va){
                switch($va['shipping_status']){
                    case 0:
                        $status = "待寄送";
                        break;
                    case 1:
                        $status = "已寄送";
                        break;
                }
                $keys = $ke+2;
                $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValueExplicit('A'.$keys, $va['order_no'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue('B'.$keys, $va['goods_name'])
                    ->setCellValue('C'.$keys, $va['goods_id'])
                    ->setCellValue('D'.$keys, $va['goods_num'])
                    ->setCellValue('E'.$keys, $va['real_price'])
                    ->setCellValue('F'.$keys, $va['order_amount'])
                    ->setCellValue('G'.$keys, $va['category_name'])
                    ->setCellValue('H'.$keys, $va['order_time'])
                    ->setCellValueExplicit('I'.$keys, $va['shipping_mobile'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue('J'.$keys, $va['consignee'])
                    ->setCellValue('K'.$keys, $va['shipping_address'])
                    ->setCellValueExplicit('K'.$keys, $va['id_card'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue('L'.$keys, $va['mall_id'].'/'.$va['mall_name'])
                    ->setCellValue('M'.$keys, $va['user_remark'])
                    ->setCellValue('N'.$keys, $status)
                    ->setCellValue('O'.$keys, $va['express_company'])
                    ->setCellValueExplicit('P'.$keys, $va['express_no'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue('Q'.$keys, $va['shipping_time'])
                    ->setCellValue('R'.$keys, $va['shipping_remark']);
            }
            $objPHPExcel->setActiveSheetIndex(0);

            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            header('pragma:public');
            header("Content-Disposition:attachment;filename=寄件列表".date('_YmdHis').".xlsx");
            $objWriter->save('php://output');exit;
        }

    }

    public function actionRefund(){
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $orderLogic = new OrderLogic();
        $this->data['breadcrumbs'] = '订单列表';
        $categoryLogic = new CategoryLogic();
        $category = $categoryLogic->getCategorys();
        if(Yii::$app->request->get('submit')!="export" && Yii::$app->request->post('submit')!="import"){
            return $this->render('refund', [
                'theme' => '/assets/',
                'category'=>$category,
                'order' => $orderLogic->getRefundOrder($query, $page),
                'query' => $query
            ]);
        }elseif(Yii::$app->request->post('submit')=="import"){
            if($_FILES['refund_file']['name']){
                LocalUpload::go_upload_file($_FILES['shipping_file']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $objPHPExcel = new \PHPExcel();
                $objPHPExcel = \PHPExcel_IOFactory::load(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$uploadresult);
                $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
                foreach($sheetData AS $key=>$value){
                    if(isset($value['C']))
                        $orderLogic->refundOrder1($value['A'],$value['B'],$value['C']);
                    else
                        $orderLogic->refundOrder1($value['A'],$value['B']);
                }
            }else{
                $uploadresult = '';
            }

        }else{
            $order = $orderLogic->getRefundOrder($query,1,null);
            $objPHPExcel = new \PHPExcel();
            $objPHPExcel->getProperties()->setCreator("中经汇通")
                ->setTitle("退款列表");
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '退款单号')
                ->setCellValue('B1', '订单号')
                ->setCellValue('C1', '支付渠道')
                ->setCellValue('D1', '支付渠道订单号')
                ->setCellValue('E1', '付款金额')
                ->setCellValue('F1', '用户手机号')
                ->setCellValue('G1', '退款状态')
                ->setCellValue('H1', '退款金额')
                ->setCellValue('I1', '退款原因')
                ->setCellValue('J1', '退款单生成时间')
                ->setCellValue('K1', '退款完成时间')
                ->setCellValue('L1', '备注');
            $status = "";
            foreach($order['lists'] AS $ke=>$va){
                switch($va['refund_status']){
                    case 0:
                        $status = "不需退款";
                        break;
                    case 1:
                        $status = "申请退款";
                        break;
                    case 2:
                        $status = "退款中";
                        break;
                    case 3:
                        $status = "拒绝退款";
                        break;
                    case 4:
                        $status = "完成退款";
                        break;
                }
                $keys = $ke+2;
                $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValueExplicit('A'.$keys, $va['refund_no'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValueExplicit('B'.$keys, $va['order_no'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue('C'.$keys, $va['pay_type'])
                    ->setCellValueExplicit('D'.$keys, $va['pay_no'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue('E'.$keys, $va['order_amount'])
                    ->setCellValueExplicit('F'.$keys, $va['mobile'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue('G'.$keys, $status)
                    ->setCellValue('H'.$keys, $va['refund_amount'])
                    ->setCellValue('I'.$keys, $va['reason'])
                    ->setCellValue('J'.$keys, $va['add_time'])
                    ->setCellValue('K'.$keys, $va['over_time'])
                    ->setCellValue('L'.$keys, $va['refund_remark']);
            }
            $objPHPExcel->setActiveSheetIndex(0);

            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            header('pragma:public');
            header("Content-Disposition:attachment;filename=退款列表".date('_YmdHis').".xlsx");
            $objWriter->save('php://output');exit;
        }

    }

    public function actionShippingOrder(){
        $order_id = Yii::$app->request->post('order_id');
        $express_company = Yii::$app->request->post('express_company');
        $express_no = Yii::$app->request->post('express_no');
        $shipping_remark = Yii::$app->request->post('shipping_remark');
        $orderLogic = new OrderLogic();
        $result = $orderLogic->shippingOrder($order_id,$express_company,$express_no,$shipping_remark);
        echo json_encode($result);
    }

    public function actionRefundOrder(){
        $refund_id = Yii::$app->request->post('refund_id');
        $refund_amount = Yii::$app->request->post('refund_amount');
        $refund_remark = Yii::$app->request->post('refund_remark');
        $orderLogic = new OrderLogic();
        $result = $orderLogic->refundOrder($refund_id,$refund_amount,$refund_remark);
        echo json_encode($result);
    }

    public function actionRejectRefund(){
        $refund_id = Yii::$app->request->get('refund_id');
        $orderLogic = new OrderLogic();
        $logicRes = $orderLogic->rejectRefund($refund_id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','拒绝成功');
        } else {
            Yii::$app->getSession()->setFlash('error','拒绝失败');
        }
        return $this->redirect(U('order/refund'));
    }




}
